package com.cheer.ba.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cheer.ba.R;
import com.cheer.ba.utils.DensityUtils;

/**
 * dialog 工具类
 */
public class DialogUtils {

    public static Dialog selfDialog(Context context, View view, boolean canceledOnTouch) {
        if (view == null) {
            return null;
        }
        Dialog mDialog = null;
        mDialog = new Dialog(context, R.style.AppTheme_DialogStyle);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                DensityUtils.getScreenW(context) - 80,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        mDialog.setContentView(view, params);
        mDialog.setCanceledOnTouchOutside(canceledOnTouch);
        return mDialog;
    }

    /**
     * 根据指定位置弹出popupwindow
     *
     * @param v
     * @param resource
     * @return
     */
    public static PopupWindow showPopByLocation(PopupWindow mPopup, final View v, View resource) {
        PopupWindow pop;
        if (null == mPopup) {
            // 创建PopupWindow对象
            pop = new PopupWindow(resource, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, false);
        } else {
            pop = mPopup;
        }
        resource.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int popupWidth = resource.getMeasuredWidth();
        int popupHeight = resource.getMeasuredHeight();
        int[] location = new int[2];
        // 需要设置一下此参数，点击外边可消失
        pop.setBackgroundDrawable(new BitmapDrawable());
        //设置点击窗口外边窗口消失
        pop.setOutsideTouchable(true);
        // 设置此参数获得焦点，否则无法点击
        pop.setFocusable(true);
        //防止虚拟软键盘被弹出菜单遮住
        pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        // 获得位置 这里的v是目标控件，就是你要放在这个v的上面还是下面
        v.getLocationOnScreen(location);
        //这里就可自定义在上方和下方了 ，这种方式是为了确定在某个位置，某个控件的左边，右边，上边，下边都可以
        pop.showAtLocation(v, Gravity.NO_GRAVITY, location[0] - popupWidth, location[1]);
        return pop;
    }

    private static PopupWindow mPopup;

    /**
     * 初始化popupwindow
     */
    public static PopupWindow initPopup(View viewContent) {
        mPopup = new PopupWindow(viewContent, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        mPopup.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x80000000);
        mPopup.setBackgroundDrawable(dw);
        mPopup.setAnimationStyle(R.style.popwindow_anim_style);
        return mPopup;
    }

    public static PopupWindow initDropPopup(View viewContent) {
        mPopup = new PopupWindow(viewContent, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        mPopup.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x80000000);
        mPopup.setBackgroundDrawable(dw);
        mPopup.setAnimationStyle(R.style.popwindow_anim_style);
        return mPopup;
    }

    /**
     * 初始化评论或者回复的弹出框
     *
     * @param rootView
     * @param layoutView
     * @return
     */
    public static PopupWindow initCommentPop(final View rootView, final View layoutView) {
        // 显示窗口
        // 创建PopupWindow对象
        mPopup = new PopupWindow(layoutView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, false);
        // 需要设置一下此参数，点击外边可消失
        mPopup.setBackgroundDrawable(new BitmapDrawable());
        //设置点击窗口外边窗口消失
        mPopup.setOutsideTouchable(true);
        // 设置此参数获得焦点，否则无法点击
        mPopup.setFocusable(true);
        //防止虚拟软键盘被弹出菜单遮住
        mPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //        pop.showAsDropDown(v);
        mPopup.showAtLocation(rootView, Gravity.BOTTOM, 0, 0);

        mPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                layoutView.setVisibility(View.GONE);
                disMissPop();
            }
        });
        hideSoft(rootView);
        return mPopup;
    }

    /**
     * 隐藏键盘
     */
    public static void hideSoft(final View v) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }, 10);
    }

    public static void disMissPop() {
        if (null != mPopup) {
            mPopup.dismiss();
        }
    }

    /**
     * 展示popup
     */
    public static void showDrawPop(View root, Activity activity) {
        if (null != mPopup && !activity.isFinishing()) {
            if (mPopup.isShowing()) {
                mPopup.dismiss();
            }
            mPopup.showAtLocation(root, Gravity.BOTTOM, 0, 0);
        }
    }

    public static Dialog mDialog;

    /**
     * 弹出登录提示框
     */
    public static void showCommDialog(Context mContext, String mTitle, String leftText, String rightText, View.OnClickListener mListener) {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.login_remind, null, false);
        TextView mTvTitle = (TextView) view.findViewById(R.id.id_remind_login_txt);
        mTvTitle.setText(mTitle);
        mTvTitle.setSingleLine(false);
        TextView mTxtCancel = (TextView) view.findViewById(R.id.id_login_cancel_txt);
        mTxtCancel.setText(leftText);
        mTxtCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
        TextView mTxtConfirm = (TextView) view.findViewById(R.id.id_login_confirm_txt);
        mTxtConfirm.setText(rightText);
        if (null != mListener) {
            mTxtConfirm.setOnClickListener(mListener);
        }
        mDialog = DialogUtils.selfDialog(mContext, view, true);
    }

    public static boolean isShow() {
        if (null != mDialog && mDialog.isShowing()) {
            return true;
        }
        return false;
    }

    public static void DialogShow() {
        if (mDialog != null && !mDialog.isShowing()) {
            mDialog.show();
        }
    }

    public static void DialogDiss() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }
}
